const app = getApp()
Component({
  data: {
    showSortMenu: false,
    sortValue: 'time',
    sortOptions: [
      { text: '时间', value: 'time' },
      { text: '按年', value: 'year' },
      { text: '按月', value: 'month' },
      { text: '名称', value: 'name' }
    ],
    contractList: [],
    showActionSheet: false,
    actionSheetActions: [
      { name: '编辑', value: 'edit' },
      { name: '快速生成工单', value: 'detail' },
      { name: '不在提醒到期', value: 'detail' },
      { name: '删除', value: 'delete', color: '#ee0a24' }
    ],
    currentContract: {}
  },
  lifetimes: {
    attached() {
      this.getContractList();
    }
  },
  methods: {
    getContractList() {
      app.axios('contract', {
        op: "ajaxContractList",
      }).then(res => {
        res.lists.forEach(item => {
          if (item.stime) {
            item.stime = app.dateFormatter(item.stime)
          }
        })
        if (res.code) {
          this.setData({
            contractList: res.lists
          })
        }
      })
    },
    onAddContract() {
      wx.navigateTo({
        url: '/pages/operation-center/add-contract/index'
      });
    },
    onSortSelect(e) {
      this.setData({ sortValue: e.currentTarget.dataset.value, showSortMenu: false });
      // 排序逻辑
    },

    onCardTap(e) {
      const item = e.currentTarget.dataset.item;
      this.setData({
        showActionSheet: true,
        currentContract: item
      });
    },
    singleClick(e) {
      const id = e.currentTarget.dataset.item.id
      if (id) {
        app.navigateTo(`pages/operation-center/paper-contract/index?id=${id}`)
      }
    },
    onCloseActionSheet() {
      this.setData({
        showActionSheet: false
      });
    },
    onActionSheetSelect(e) {
      const currentContract = this.data.currentContract
      const { value } = e.detail;
      if (value === 'detail') {
        wx.showModal({
          title: '合同详情',
          content: `标题：${this.data.currentContract.title}\n客户：${this.data.currentContract.customer}\n公司：${this.data.currentContract.company}\n负责人：${this.data.currentContract.owner}\n编号：${this.data.currentContract.code}\n备注：${this.data.currentContract.remark}\n时间：${this.data.currentContract.date}`,
          showCancel: false
        });
      } else if (value === 'edit') {
        app.navigateTo(`/pages/operation-center/add-contract/index?id=${currentContract.id}`)
      } else if (value === 'delete') {
        const alert = this.selectComponent('#v-alert')
        alert.toggleAlert()
      }
      this.setData({ showActionSheet: false });
    },
    handleDelete() {
      const currentContract = this.data.currentContract
      app.axios('contract', {
        op: 'ajaxContractDel',
        id: currentContract.id
      }).then(res => {
        if (res.code) {
          wx.showToast({ title: '删除合同成功', icon: 'success' });
          this.getContractList()
        }
      })
    }
  }
}) 